class MigratePagesSecurity < ActiveRecord::Migration

  class << self
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    # Data insert/delete methods
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def up
      init_roles
      add_acl_data
    end

    def down
      init_roles
      delete_acl_data
    end

    private
      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      # Private data insert/delete methods
      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

      def init_roles
        @@admin = Role.find(1)
        #@@user = Role.find(2)
      end

      def add_acl_data
        add_content_management_data
        add_public_data
        add_common_user_access_data
      end

      def add_content_management_data
        sobj = SystemObject.create(:name => "Content Management")
        @@admin.add_system_object(sobj) 

        SystemMethod.create(:name => "pages/spell", :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/pages/list", :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/pages/create", :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/pages/show", :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/pages/update", :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/pages/delete", :system_object_id => sobj.id)
        SystemMethod.create(:name => "manage/folders/index", :system_object_id => sobj.id)
      end

      def add_public_data
        sobj = SystemObject.find(:first, :conditions => "name = 'Public'")
        SystemMethod.create(:name => "home/index", :system_object_id => sobj.id)
      end

      def add_common_user_access_data
        sobj = SystemObject.find(:first, :conditions => "name = 'Common User Access'")
        SystemMethod.create(:name => 'pages/show', :system_object_id => sobj.id)
      end

      def delete_acl_data
        delete_public_data
        delete_common_user_access_data
        delete_content_management_data
      end

      def delete_content_management_data
        sobj = SystemObject.find(:first, :conditions => "name = 'Content Management'")
        @@admin.delete_system_object(sobj) 
        sobj.destroy
      end

      def delete_public_data
        sobj = SystemObject.find(:first, :conditions => "name = 'Public'")
        SystemMethod.delete_all("system_object_id = #{sobj.id} and name = 'home/index'")
      end

      def delete_common_user_access_data
        sobj = SystemObject.find(:first, :conditions => "name = 'Common User Access'")

        SystemMethod.delete_all("system_object_id = #{sobj.id} and name = 'pages/show'")
      end
  end #class block
end
